package com.atguigu.jue.demo02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProdConsumerDemo {
    public static void main(String[] args) {
        AirCondition airCondition=new AirCondition();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
/**
 * @auther zzyy
 * @create 2020-03-26 11:41
 *
 * 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10次
 *
 * 1 高内聚低耦合的前提下，线程       操作    资源类
 * 2 判断、干活、通知
 * 3 防止线程的虚假唤醒，判断推荐使用while而不是单单的if判断
 */
class AirCondition{
    int number=0;
    Lock lock=new ReentrantLock();
    Condition condition=lock.newCondition();
//    public synchronized void increment() throws InterruptedException {
//        //判断
//        while (number!=0){
//            this.wait();
//        }
//        //干活
//        System.out.println(Thread.currentThread().getName()+"\t"+(++number));
//        //通知
//        this.notifyAll();
//    }
//    public synchronized void decrement() throws InterruptedException {
//        //判断
//        while (number==0){
//            this.wait();
//        }
//        //干活
//        System.out.println(Thread.currentThread().getName()+"\t"+(--number));
//        //通知
//        this.notifyAll();
//    }
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number!=0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            //通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number==0){
                condition.await();
            }
            //干活
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
            //通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}